package singleton.practice_01;

/**
 * @author yi qiang
 * @date 2021/9/12 14:48
 */
public class SingletonType02 {

    public static void main(String[] args) {
        Singleton3.getInstance().test();
        Singleton4.getInstance().test();
        Singleton5.getInstance().test();
    }

}

class Singleton3 {

    private Singleton3() {
    }

    private static Singleton3 instance;

    public static synchronized Singleton3 getInstance() {
        if (instance == null) {
            instance = new Singleton3();
        }
        return instance;
    }

    public void test() {
        System.out.println("test");
    }

}

class Singleton4 {

    private Singleton4() {
    }

    private static volatile Singleton4 instance;

    public static synchronized Singleton4 getInstance() {
        if (instance == null) {
            synchronized (Singleton4.class) {
                if (instance == null) {
                    instance = new Singleton4();
                }
            }
        }
        return instance;
    }

    public void test() {
        System.out.println("test");
    }

}

class Singleton5{

    private Singleton5() {
    }

    private static class Singleton5Inner{
        private static final Singleton5 SINGLETON5 =new Singleton5();
    }

    public static synchronized Singleton5 getInstance() {
       return Singleton5Inner.SINGLETON5;
    }

    public void test() {
        System.out.println("test");
    }
}
